package com.ht.dsp.test;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Created by IntelliJ IDEA.
 * User:heguohong
 * Date: 2008-11-17
 * Time: 13:40:22
 * To change this template use File | Settings | File Templates.
 */
public class FileCopy {
    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

    public static void main(String[] args) throws IOException, ParseException {
        FileCopy afile = new FileCopy();
        List<Long> r = afile.readfile(new File(args[0]));
        int interval = 60000;
        String p = System.getProperty("interval");
        if (p != null)
            interval = Integer.parseInt(p);
        int i = 0;
        while (true) {
            //todo:
            String source = "output" + format.format(r.get(i));
            try {
                File inputFile = new File(args[0] + "/" + source);
                if ((inputFile.length() / 1024) > 90) {
                    File outputFile = new File(args[1] + "/" + source);
                    FileReader in = new FileReader(inputFile);
                    FileWriter out = new FileWriter(outputFile);
                    int c;
                    while ((c = in.read()) != -1) {
                        out.write(c);
                    }
                    in.close();
                    out.close();
                    System.out.println("File copyed: " + source + " at " + format.format(System.currentTimeMillis()));
                    Thread.sleep(interval);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i++;
            if (i == r.size()) {
                i = 0;
            }
        }
    }

    private List<Long> readfile(File afile) throws ParseException {
        String[] filelist = afile.list();
        List<Long> name = new ArrayList<Long>();
        for (String s : filelist) {
            File file = new File(s);
            name.add(format.parse(file.getName().substring(6)).getTime());
        }
        Collections.sort(name);
        return name;
    }
}


